#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
交易 - C2B二维码支付预授权相关模型
"""

from typing import Dict, Any
from jlpay.model.base import OrgBaseReq, BaseResponse


class QrcodePayPreAuthRequest(OrgBaseReq):
    """二维码支付预授权请求模型"""
    
    API_URL = "/open/trans/qrcodepaypreauth"
    
    def __init__(self):
        """初始化二维码支付预授权请求"""
        super().__init__()
        self.mch_id = None  # 商户号
        self.term_no = None  # 终端号
        self.pay_type = None  # 交易类型
        self.out_trade_no = None  # 外部订单号
        self.payment_valid_time = None  # 订单有效期
        self.body = None  # 商品标题
        self.attach = None  # 商品描述
        self.total_fee = None  # 交易金额
        self.remark = None  # 备注
        self.notify_url = None  # 通知地址
        self.op_user_id = None  # 操作员
        self.op_shop_id = None  # 门店号
        self.device_info = None  # 终端设备号
        self.mch_create_ip = None  # 商户IP
        self.trans_address = None  # 交易地址
        self.area_code = None  # 交易地区码
        self.longitude = None  # 经度
        self.latitude = None  # 纬度
        self.sub_appid = None  # 子商户appid
        self.terminal_info = None  # 终端信息
    
    def set_mch_id(self, mch_id):
        """
        设置商户号
        
        Args:
            mch_id: 商户号
            
        Returns:
            QrcodePayPreAuthRequest实例
        """
        self.mch_id = mch_id
        return self
    
    def set_term_no(self, term_no):
        """
        设置终端号
        
        Args:
            term_no: 终端号
            
        Returns:
            QrcodePayPreAuthRequest实例
        """
        self.term_no = term_no
        return self
    
    def set_pay_type(self, pay_type):
        """
        设置交易类型
        
        Args:
            pay_type: 交易类型
            
        Returns:
            QrcodePayPreAuthRequest实例
        """
        self.pay_type = pay_type
        return self
    
    def set_out_trade_no(self, out_trade_no):
        """
        设置外部订单号
        
        Args:
            out_trade_no: 外部订单号
            
        Returns:
            QrcodePayPreAuthRequest实例
        """
        self.out_trade_no = out_trade_no
        return self
    
    def set_payment_valid_time(self, payment_valid_time):
        """
        设置订单有效期
        
        Args:
            payment_valid_time: 订单有效期
            
        Returns:
            QrcodePayPreAuthRequest实例
        """
        self.payment_valid_time = payment_valid_time
        return self
    
    def set_body(self, body):
        """
        设置商品标题
        
        Args:
            body: 商品标题
            
        Returns:
            QrcodePayPreAuthRequest实例
        """
        self.body = body
        return self
    
    def set_attach(self, attach):
        """
        设置商品描述
        
        Args:
            attach: 商品描述
            
        Returns:
            QrcodePayPreAuthRequest实例
        """
        self.attach = attach
        return self
    
    def set_total_fee(self, total_fee):
        """
        设置交易金额
        
        Args:
            total_fee: 交易金额
            
        Returns:
            QrcodePayPreAuthRequest实例
        """
        self.total_fee = total_fee
        return self
    
    def set_remark(self, remark):
        """
        设置备注
        
        Args:
            remark: 备注
            
        Returns:
            QrcodePayPreAuthRequest实例
        """
        self.remark = remark
        return self
    
    def set_notify_url(self, notify_url):
        """
        设置通知地址
        
        Args:
            notify_url: 通知地址
            
        Returns:
            QrcodePayPreAuthRequest实例
        """
        self.notify_url = notify_url
        return self
    
    def set_op_user_id(self, op_user_id):
        """
        设置操作员
        
        Args:
            op_user_id: 操作员
            
        Returns:
            QrcodePayPreAuthRequest实例
        """
        self.op_user_id = op_user_id
        return self
    
    def set_op_shop_id(self, op_shop_id):
        """
        设置门店号
        
        Args:
            op_shop_id: 门店号
            
        Returns:
            QrcodePayPreAuthRequest实例
        """
        self.op_shop_id = op_shop_id
        return self
    
    def set_device_info(self, device_info):
        """
        设置终端设备号
        
        Args:
            device_info: 终端设备号
            
        Returns:
            QrcodePayPreAuthRequest实例
        """
        self.device_info = device_info
        return self
    
    def set_mch_create_ip(self, mch_create_ip):
        """
        设置商户IP
        
        Args:
            mch_create_ip: 商户IP
            
        Returns:
            QrcodePayPreAuthRequest实例
        """
        self.mch_create_ip = mch_create_ip
        return self
    
    def set_trans_address(self, trans_address):
        """
        设置交易地址
        
        Args:
            trans_address: 交易地址
            
        Returns:
            QrcodePayPreAuthRequest实例
        """
        self.trans_address = trans_address
        return self
    
    def set_area_code(self, area_code):
        """
        设置交易地区码
        
        Args:
            area_code: 交易地区码
            
        Returns:
            QrcodePayPreAuthRequest实例
        """
        self.area_code = area_code
        return self
    
    def set_longitude(self, longitude):
        """
        设置经度
        
        Args:
            longitude: 经度
            
        Returns:
            QrcodePayPreAuthRequest实例
        """
        self.longitude = longitude
        return self
    
    def set_latitude(self, latitude):
        """
        设置纬度
        
        Args:
            latitude: 纬度
            
        Returns:
            QrcodePayPreAuthRequest实例
        """
        self.latitude = latitude
        return self
    
    def set_sub_appid(self, sub_appid):
        """
        设置子商户appid
        
        Args:
            sub_appid: 子商户appid
            
        Returns:
            QrcodePayPreAuthRequest实例
        """
        self.sub_appid = sub_appid
        return self
    
    def set_terminal_info(self, terminal_info):
        """
        设置终端信息
        
        Args:
            terminal_info: 终端信息
            
        Returns:
            QrcodePayPreAuthRequest实例
        """
        self.terminal_info = terminal_info
        return self
    
    def to_dict(self) -> Dict[str, Any]:
        """
        转换为字典格式
        
        Returns:
            字典格式的数据
        """
        data = super().to_dict()
        data.update({
            'mch_id': self.mch_id,
            'term_no': self.term_no,
            'pay_type': self.pay_type,
            'out_trade_no': self.out_trade_no,
            'payment_valid_time': self.payment_valid_time,
            'body': self.body,
            'attach': self.attach,
            'total_fee': self.total_fee,
            'remark': self.remark,
            'notify_url': self.notify_url,
            'op_user_id': self.op_user_id,
            'op_shop_id': self.op_shop_id,
            'device_info': self.device_info,
            'mch_create_ip': self.mch_create_ip,
            'trans_address': self.trans_address,
            'area_code': self.area_code,
            'longitude': self.longitude,
            'latitude': self.latitude,
            'sub_appid': self.sub_appid,
            'terminal_info': self.terminal_info.to_dict() if self.terminal_info else None
        })
        return data


class QrcodePayPreAuthResponse(BaseResponse):
    """二维码支付预授权响应"""
    
    def __init__(self, response_dict=None):
        """
        初始化二维码支付预授权响应
        
        Args:
            response_dict: 响应字典
        """
        super().__init__(response_dict)
        self.qrcode_url = None  # 二维码链接
        self.qrcode_content = None  # 二维码内容
        self.out_trade_no = None  # 外部订单号
        self.transaction_id = None  # 平台订单号
        
        if response_dict:
            self.qrcode_url = response_dict.get('qrcode_url')
            self.qrcode_content = response_dict.get('qrcode_content')
            self.out_trade_no = response_dict.get('out_trade_no')
            self.transaction_id = response_dict.get('transaction_id')
    
    def to_dict(self) -> Dict[str, Any]:
        """
        转换为字典格式
        
        Returns:
            字典格式的数据
        """
        data = super().to_dict()
        data.update({
            'qrcode_url': self.qrcode_url,
            'qrcode_content': self.qrcode_content,
            'out_trade_no': self.out_trade_no,
            'transaction_id': self.transaction_id
        })
        return data 